package com.nit.service.impl;

import com.nit.dto.GoodsDTO;
import com.nit.dto.utils.PaginationDTO;
import com.nit.dto.utils.TableRequestDTO;
import com.nit.dto.utils.TableResponseDTO;
import com.nit.mapper.ShopGoodsMapper;
import com.nit.service.ShopGoodsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ShopGoodsServiceImpl implements ShopGoodsService {

    @Resource
    private ShopGoodsMapper shopGoodsMapper;

    @Override
    public TableResponseDTO query(TableRequestDTO tableRequestDTO) throws Exception {
        Long count = shopGoodsMapper.count4Table(
                tableRequestDTO.getJsonSearch().get("goodsName"),
                tableRequestDTO.getJsonSearch().get("goodsCate")
        );

        List<GoodsDTO> goodsDTOS = shopGoodsMapper.list4Table(
                tableRequestDTO.getStart(count),
                tableRequestDTO.getPageSize(),
                tableRequestDTO.getJsonSearch().get("goodsName"),
                tableRequestDTO.getJsonSearch().get("goodsCate")
        );

        TableResponseDTO tableResponseDTO = new TableResponseDTO(goodsDTOS,
                new PaginationDTO(tableRequestDTO.getPageSize(),
                        count, tableRequestDTO.getCurrentPage(),
                        String.valueOf(tableRequestDTO.getSearch())));

        return tableResponseDTO;
    }
}
